4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
11 // You must not remove this notice, or any other, from this software.
16 namespace Microsoft
.JScript
{
19 using System
.Reflection
;
20 using System
.Reflection
.Emit
;
22 internal abstract class EnumWrapper
: IConvertible
{
23 internal abstract Object
value {
27 internal abstract Type type
{
31 internal abstract String name
{
35 internal virtual IReflect classScopeOrType
{
41 TypeCode IConvertible
.GetTypeCode(){
42 return Convert
.GetTypeCode(this.value);
45 bool IConvertible
.ToBoolean(IFormatProvider provider
){
46 return ((IConvertible
)this.value).ToBoolean(provider
);
49 char IConvertible
.ToChar(IFormatProvider provider
){
50 return ((IConvertible
)this.value).ToChar(provider
);
53 sbyte IConvertible
.ToSByte(IFormatProvider provider
){
54 return ((IConvertible
)this.value).ToSByte(provider
);
57 byte IConvertible
.ToByte(IFormatProvider provider
){
58 return ((IConvertible
)this.value).ToByte(provider
);
61 short IConvertible
.ToInt16(IFormatProvider provider
){
62 return ((IConvertible
)this.value).ToInt16(provider
);
65 ushort IConvertible
.ToUInt16(IFormatProvider provider
){
66 return ((IConvertible
)this.value).ToUInt16(provider
);
69 int IConvertible
.ToInt32(IFormatProvider provider
){
70 return ((IConvertible
)this.value).ToInt32(provider
);
73 uint IConvertible
.ToUInt32(IFormatProvider provider
){
74 return ((IConvertible
)this.value).ToUInt32(provider
);
77 long IConvertible
.ToInt64(IFormatProvider provider
){
78 return ((IConvertible
)this.value).ToInt64(provider
);
81 ulong IConvertible
.ToUInt64(IFormatProvider provider
){
82 return ((IConvertible
)this.value).ToUInt64(provider
);
85 float IConvertible
.ToSingle(IFormatProvider provider
){
86 return ((IConvertible
)this.value).ToSingle(provider
);
89 double IConvertible
.ToDouble(IFormatProvider provider
){
90 return ((IConvertible
)this.value).ToDouble(provider
);
93 Decimal IConvertible
.ToDecimal(IFormatProvider provider
){
94 return ((IConvertible
)this.value).ToDecimal(provider
);
97 DateTime IConvertible
.ToDateTime(IFormatProvider provider
){
98 return ((IConvertible
)this.value).ToDateTime(provider
);
101 String IConvertible
.ToString(IFormatProvider provider
){
102 return ((IConvertible
)this.value).ToString(provider
);
105 Object IConvertible
.ToType(Type conversionType
, IFormatProvider provider
){
106 return ((IConvertible
)this.value).ToType(conversionType
, provider
);
109 internal Object
ToNumericValue(){
113 public override String
ToString(){
114 if (this.name
!= null) return this.name
;
115 FieldInfo
[] fields
= this.type
.GetFields(BindingFlags
.Static
|BindingFlags
.Public
);
116 foreach (FieldInfo field
in fields
)
117 if (StrictEquality
.JScriptStrictEquals(this.value, field
.GetValue(null))) return field
.Name
;
118 return Convert
.ToString(this.value);
123 internal sealed class DeclaredEnumValue
: EnumWrapper
{
124 private String _name
;
125 internal ClassScope _classScope
;
126 internal Object _value
;
128 internal DeclaredEnumValue(Object
value, String name
, ClassScope classScope
){
130 this._classScope
= classScope
;
134 internal override Object
value {
140 internal override Type type
{
142 return this._classScope
.GetTypeBuilderOrEnumBuilder();
146 internal override String name
{
152 internal override IReflect classScopeOrType
{
154 return this._classScope
;
158 internal void CoerceToBaseType(Type bt
, Context errCtx
){
160 AST pval
= ((AST
)this.value).PartiallyEvaluate();
161 if (pval
is ConstantWrapper
)
162 val
= ((ConstantWrapper
)pval
).Evaluate();
164 pval
.context
.HandleError(JSError
.NotConst
);
166 this._value
= Convert
.CoerceT(val
, bt
);
168 errCtx
.HandleError(JSError
.TypeMismatch
);
169 this._value
= Convert
.CoerceT(0, bt
);
175 internal sealed class MetadataEnumValue
: EnumWrapper
{
177 private Object _value
;
179 internal static Object
GetEnumValue(Type type
, Object
value) {
180 Debug
.Assert(type
.IsEnum
);
181 if (!type
.Assembly
.ReflectionOnly
)
182 return Enum
.ToObject(type
, value);
183 return new MetadataEnumValue(type
, value);
186 private MetadataEnumValue(Type type
, Object
value) {
191 internal override Object
value {
197 internal override Type type
{
203 internal override String name
{
205 String name
= Enum
.GetName(this._type
, this._value
);
207 name
= this._value
.ToString();